package exam.quarzt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import exam.quarzt.domain.SysJob;
import exam.quarzt.service.SysJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/4/8 16:20
 * @since JDK 1.8
 */
@RestController
@Api(tags = {"定时job模块测试"})
@RequestMapping("jobtest")
public class SysJobTestController {

    @Autowired
    private SysJobService sysJobService;

    @PostMapping("update")
    @ApiOperation("job更新")
    public JSONObject updateController(@ApiParam("修改的id")@RequestParam("id")String id,
                                       @ApiParam("任务名")@RequestParam("name")String name){
        JSONObject jsonObject=new JSONObject();
        SysJob sysJob=sysJobService.getById(id);
        if(sysJob==null){
            jsonObject.put("code",1);
            jsonObject.put("msg","error");
            return jsonObject;
        }
        sysJob.setJobname(name);
        sysJobService.updateById(sysJob);
       /* sysJobService.update(Wrappers.<SysJob>lambdaUpdate()
        .set(SysJob::getJobname,name).eq(SysJob::getId,id));*/

        jsonObject.put("code",0);
        jsonObject.put("msg","succeess");
        return jsonObject;
    }

    @DeleteMapping()
    @ApiOperation("逻辑删除")
    public JSONObject deleteController(@ApiParam("删除的id")@RequestParam("id")String id){
       boolean flage=sysJobService.removeById(id);
       JSONObject jsonObject=new JSONObject();
       jsonObject.put("code",0);
       jsonObject.put("data",flage);
       return jsonObject;
    }

    @GetMapping("listtest")
    @ApiOperation("定时任务分页获取")
    public JSONObject getPageListControlelr(@ApiParam("页码")@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                            @ApiParam("条数")@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
       IPage<SysJob> sysJobIPage=sysJobService.page(new Page(pageNum,pageSize), Wrappers.<SysJob>lambdaQuery()
        .orderByDesc(SysJob::getCreateTime));
       JSONObject jsonObject=new JSONObject();
       jsonObject.put("code",0);
       jsonObject.put("msg","success");
       jsonObject.put("data",sysJobIPage);
       return jsonObject;
    }


    @PostMapping("addjobTest")
    @ApiOperation("定时任务测试添加")
    public JSONObject addJobTestController(@ApiParam("任务名称") @RequestParam("name")String name,
                                       @ApiParam("任务分组")@RequestParam("group")String group,
                                       @ApiParam(value = "任务类型",required = false)@RequestParam(value = "type",required = false)String type,
                                       @ApiParam(value = "类型编码",required = false)@RequestParam(value = "typecode",required = false)String typecode,
                                           @ApiParam("反射路径")@RequestParam("clazz")String clazz,
                                           @ApiParam("cron表达式")@RequestParam("cron")String cron,
                                           @ApiParam("rest调用")@RequestParam("rest")String rest,
                                           @ApiParam("参数，目前只支持一个")@RequestParam(value = "param",required = false)String param){
        SysJob sysJob=new SysJob();
        sysJob.setClazz(clazz);
        sysJob.setJobname(name);
        sysJob.setCron(cron);
        sysJob.setRest(rest);
        sysJob.setJobgroup(group);
        JSONArray jsonArray=new JSONArray();
        if(!StringUtils.isEmpty(param)){
            jsonArray.add(param);
        }
        sysJob.setMethodName(jsonArray.toJSONString());
        sysJobService.save(sysJob);
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","success");
        return  jsonObject;
    }
}
